package net.tatu.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.tatu.base.Page;
import net.tatu.dao.ModelLevelMapper;
import net.tatu.dao.ModelMapper;
import net.tatu.dao.ModelOperateMapper;
import net.tatu.dao.UserMapper;
import net.tatu.model.Model;
import net.tatu.model.ModelLevel;
import net.tatu.model.ModelOperate;
import net.tatu.model.User;
import net.tatu.service.ModelService;
import net.tatu.service.UserService;
import net.tatu.util.Encrypt;
import net.tatu.util.Util;

@Service
public class ModelServiceImpl implements ModelService {
	Logger log = Logger.getLogger(ModelServiceImpl.class);
	@Autowired
	private ModelMapper modelMapper;
	@Autowired
	private ModelLevelMapper modelLevelMapper;
	@Autowired
	private ModelOperateMapper modelOperateMapper;
	@Override
	public boolean addModel(Model model) {
		boolean ret = false;
		int i = modelMapper.insertModel(model);
		if(i == 1){
			ret = true;
		}
		return ret;
	}
	@Override
	public Model get(Model model) {
		// TODO Auto-generated method stub
		return modelMapper.get(model);
	}
	@Override
	public boolean updateModel(Model model) {
		boolean ret = false;
		int i = modelMapper.updateModel(model);
		if(i == 0){
			ret = true;
		}
		return ret;
	}
	@Override
	public Page getModel(String level, String target, String typeName, int pageSize, int startIndex) {
		int le = 0;
		int ta = 0;
		if(null != level){
			le = Integer.valueOf(level);
		}
		if(null != target){
			ta = Integer.valueOf(target);
		}
		
		List<Model> modList = modelMapper.getModel(le, ta, typeName, pageSize, startIndex);
		int totalCount = modelMapper.countModel(le, ta, typeName, pageSize, startIndex);
		Page page = new Page(modList, totalCount, pageSize, startIndex);
		return page;
	}
	@Override
	public List<ModelLevel> getModelLevel() {
		List<ModelLevel> modelLevel = null;
		try{
			modelLevel = modelLevelMapper.getModelLevels();
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return modelLevel;
	}
	@Override
	public boolean modelGood(String modelId, User user, String msg) {
		boolean ret = false;
		ModelOperate modelOperate = new ModelOperate();
		modelOperate.setModelId(Long.valueOf(modelId));
		modelOperate.setUserId(user.getUserId());
		ModelOperate mo = modelOperateMapper.get(modelOperate);
		if(null != mo){
			if(mo.getIsGoodFlag() == 1){
				msg = "you have good this";
			}else{
				mo.setIsGoodFlag(1);
				mo.setRecordStatusCode(1);
				int i = modelOperateMapper.updateModelOperate(mo);
				if(i == 1){
					ret = true;
				}
			}
			
		}else{
			modelOperate.setIsGoodFlag(1);
			modelOperate.setRecordStatusCode(1);
			int i = modelOperateMapper.insertModelOperate(modelOperate);
			if(i == 1){
				ret = true;
			}
		}
		return ret;
	}
	@Override
	public boolean modelCollect(String modelId, User user, String msg) {
		boolean ret = false;
		ModelOperate modelOperate = new ModelOperate();
		modelOperate.setModelId(Long.valueOf(modelId));
		modelOperate.setUserId(user.getUserId());
		ModelOperate mo = modelOperateMapper.get(modelOperate);
		if(null != mo){
			if(mo.getIsCollectFlag() == 1){
				msg = "you have collect this";
			}else{
				mo.setIsCollectFlag(1);
				mo.setRecordStatusCode(1);
				int i = modelOperateMapper.updateModelOperate(mo);
				if(i == 1){
					ret = true;
				}
			}
			
		}else{
			modelOperate.setIsCollectFlag(1);
			modelOperate.setRecordStatusCode(1);
			int i = modelOperateMapper.insertModelOperate(modelOperate);
			if(i == 1){
				ret = true;
			}
		}
		return ret;
	}
	@Override
	public boolean download(String modelId, User user) {
		boolean ret = false;
		ModelOperate modelOperate = new ModelOperate();
		modelOperate.setModelId(Long.valueOf(modelId));
		modelOperate.setUserId(user.getUserId());
		ModelOperate mo = modelOperateMapper.get(modelOperate);
		if(null != mo){
			if(mo.getIsFirstDownloadFlag() == 1){
				
			}else{
				mo.setIsFirstDownloadFlag(1);
				mo.setRecordStatusCode(1);
				int i = modelOperateMapper.updateModelOperate(mo);
				if(i == 1){
					ret = true;
				}
			}
			
		}else{
			modelOperate.setIsFirstDownloadFlag(1);
			modelOperate.setRecordStatusCode(1);
			int i = modelOperateMapper.insertModelOperate(modelOperate);
			if(i == 1){
				ret = true;
			}
		}
		return ret;
	}
	@Override
	public Model getModelById(String modelId) {
		Model model = new Model();
		model.setModelId(Long.valueOf(modelId));
		Model m = modelMapper.get(model);
		return m;
	}
	@Override
	public List<Model> getSameTargetModelById(String modelId) {
		List<Model> list = modelMapper.getSamTargetModelById(Long.valueOf(modelId));
		return list;
	}
	@Override
	public ModelLevel getModelLevelById(String modelId) {
		Model model = getModelById(modelId);
		ModelLevel modelLevel = new ModelLevel();
		modelLevel.setModelLevelCode(model.getModelLevelCode());
		ModelLevel ml = modelLevelMapper.get(modelLevel);
		System.out.println("mmmmmm:" + ml.toString());
		return ml;
	}
	

	
}
